package cn.tsmchina.one.dao.user;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.tsmchina.one.dao.BaseDaoSupport;
import cn.tsmchina.one.domain.user.Customer;

@Repository
public class CustomerDaoImpl extends BaseDaoSupport<Customer> implements CustomerDao {
	@Autowired 
	private SessionFactory sessionFactory;

	public void addCustomer(Customer user) {
		super.add(user);
	}

	public void updateCustomer(Customer user) {
		super.update(user);
	}

	@SuppressWarnings("unchecked")
	public Customer getCustomerByLoginId(Integer loginId) {
		if(null != loginId){
			List<Customer> list = sessionFactory.getCurrentSession().
				createQuery("from Customer where loginId = '"+loginId+"'").list();
			if(null!=list && list.size()>0){
				return list.get(0);
			}
		}
		
		return null;
	}

	public List<Customer> getAllCustomers() {
		String hql = "from Customer c where c.type is null";
		return lists(hql);
	}

	@Override
	public Customer getCustomers(String userName, String password)
	{
		String hql = "from Customer where loginId = ? and loginPwd = ?";
		return (Customer) find(hql,Integer.parseInt(userName),password);
	}

	@Override
	public void deleteCustomer(long customerId) {
		super.delete(customerId);
	}

	@Override
	public Customer getCustomerById(long id) {
		return findById(id);
	}
	
}
